//===============================================================================
// Microsoft patterns & practices
// Web Client Software Factory
//-------------------------------------------------------------------------------
// Copyright (C) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//-------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================
using System;
using System.Collections;
using System.Collections.Generic;

namespace WCSFContrib.PageFlow.WorkflowFoundation
{
	/// <summary>
	/// A Singleton implementation of an adapter pattern 
	/// used to communicate with Workflow Foundation
	/// </summary>
	[Serializable]
	public class WorkflowDataAdapter
	{
		static WorkflowDataAdapter _instance;
		private Dictionary<Guid, object> _returnedData;
		private static object _instanceLock = new object();

		/// <summary>
		/// Static constructor used to initialize the singleton instance
		/// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline")]
        static WorkflowDataAdapter()
		{
			if (_instance == null)
			{
				lock (_instanceLock)
				{
					if (_instance == null)
					{
						_instance = new WorkflowDataAdapter();
					}
				}
			}
		}
		
		/// <summary>
		/// Gets the one instance of a WorkflowDataAdapter.
		/// </summary>
		public static WorkflowDataAdapter Instance
		{
			get { return _instance; }
		}

		/// <summary>
		/// Creates an instance of a WorkflowDataAdapter.
		/// This constructor is private to only allow access
		/// to a single instance via the static Instance.
		/// </summary>
		private WorkflowDataAdapter()
		{
			_returnedData = new Dictionary<Guid, object>();
		}
		
		/// <summary>
		/// Event handler for the <see cref="DataExchangeService">DataExchangeService</see>'s
		/// DataReturned event.
		/// </summary>
		/// <param name="sender">The object that raised the event</param>
		/// <param name="args">A DataReturnedEventArgs instance that contains the returned data.</param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers")]
        public void HandleDataReturned(object sender, DataReturnedEventArgs args)
		{
			lock (_returnedData)
			{
				_returnedData[args.InstanceId] = args.Value;
			}
		}

		/// <summary>
		/// Event handler for the <see cref="DataExchangeService">DataExchangeService</see>'s
		/// DataReturned event.
		/// </summary>
		/// <param name="sender">The object that raised the event</param>
		/// <param name="args">A KeysReturnedEventArgs instance that contains the returned keys.</param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers")]
        public void HandleKeyReturned(object sender, KeysReturnedEventArgs args)
		{
			lock(_returnedData)
			{
				_returnedData[args.InstanceId] = args.Keys;
			}
		}

		/// <summary>
		/// Gets the data returned the last time the HandleDataReturned event handler was called.
		/// </summary>
		/// <param name="instanceId">The workflow instance identifier.</param>
		/// <returns>The data requested.</returns>
		public object GetData(Guid instanceId)
		{
			object returnValue = _returnedData[instanceId];
			_returnedData.Remove(instanceId);
			return returnValue;
		}

		/// <summary>
		/// Gets the keys returned the last time the HandleKeysReturned event handler was called.
		/// </summary>
		/// <param name="instanceId">The workflow instance identifier.</param>
		/// <returns>A collection of keys.</returns>
		public ICollection GetKeys(Guid instanceId)
		{
			object returnValue = _returnedData[instanceId];
			_returnedData.Remove(instanceId);
			return (ICollection) returnValue;
		}
	}
}
